package com.my.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.my.trace.TrackPoints;

/**
 * Created by vic on 11/6/13.
 */
public class ChartView extends SurfaceView{
    final String TAG = "ChartView";
    //
    ShowThread showThread;
    SurfaceHolder surfaceHolder;
    ChartViewCallBack chartViewCallBack;
    LineChart lineChart;

    TrackPoints trackPoints;

    boolean bUpdate = false;
    //
    public ChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //
        Log.d(TAG, "ChartView constructor");
        surfaceHolder = getHolder();
        chartViewCallBack = new ChartViewCallBack();
        surfaceHolder.addCallback(chartViewCallBack);

        lineChart = new LineChart(Color.YELLOW);

        showThread = new ShowThread();
        //showThread.start();
    }

  //   @Override
 //   protected void onAttachedToWindow (){
 //       Log.d(TAG, "onAttachedToWindow");

 //   }
 //   @Override
 //   protected void onDetachedFromWindow (){
 //       Log.d(TAG, "onDetachedFromWindow");
 //   }
    //
    public void update(TrackPoints points){
        bUpdate = true;
        trackPoints = points;
    }
    //
    private void doDraw(Canvas canvas){
        lineChart.drawLine(canvas, trackPoints, 0, trackPoints.getSize());
    }
    //
    public class ChartViewCallBack implements SurfaceHolder.Callback{

        public ChartViewCallBack(){
            Log.d(TAG, "ChartViewCallBack Construct");
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated");
            showThread.start();
            //showThread.bRun = true;
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.d(TAG, "surfaceChanged");
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed");

            showThread.interrupt();
            //showThread.bRun = false;
        }
    }

    //
    public class ShowThread extends Thread {
        int span = 1000;

        //constructor
        public ShowThread() {
        }
        //
        public void run() {
            try {
                while(!isInterrupted()) {
                //while(bRun){
                    Log.d(TAG, "run");
                    if(bUpdate){
                        Canvas canvas = surfaceHolder.lockCanvas();
                        if(canvas == null)  Log.d(TAG, "canvas null");

                        doDraw(canvas);

                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                    Thread.sleep(span);

                }

                //interrupt();

            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d(TAG, "interrupted");
        }
    }
}
/*
public class ChartView extends SurfaceView implements SurfaceHolder.Callback{
    final String TAG = "ChartView";
    //
    SurfaceHolder surfaceHolder;
    LineChart lineChart = null;
    int index = 0;
    int size = 0;

    TrackPoints trackPoints;
    //ArrayList<TrackPoint> trackLog = null;
    //TrackPoint[] trackPoint = new TrackPoint[3];
    final int MSG_DRAW = 1;

    ShowThread showThread;
    Handler showHandle = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "handleMessage " + msg);

            synchronized(surfaceHolder) {
                Canvas canvas = surfaceHolder.lockCanvas();

                doDraw(canvas);

                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    };

    boolean bUpdated = false;

    //
    public ChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        surfaceHolder =  this.getHolder();//
        surfaceHolder.addCallback(this);//get holder

        lineChart = new LineChart(Color.WHITE);

       // for(int i=0; i<trackPoint.length; i++){
       //     trackPoint[i] = new TrackPoint();
       //     trackPoint[i].altitude = i*10;
       // }

        showThread = new ShowThread();

    }

    public void setTrackPoints(TrackPoints points){
        if (points == null){
            Log.d(TAG, "setTrackPoints null");
        }

        trackPoints = points;
    }

    public void setLineChart(int color){

        lineChart = new LineChart(color);
    }

    public void update(){
        bUpdated = true;
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated");
        //
        surfaceHolder =  this.getHolder();//
        surfaceHolder.addCallback(this);//get holder
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG, "surfaceChanged" );

        showThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG, "surfaceDestroyed");

        showThread.interrupt();
    }

    public void doDraw(Canvas canvas){
        if(trackPoints.getSize() <= 0)  return;

        size = trackPoints.getSize();
        Log.d(TAG, "draw " + size);

        //lineChart.drawLine(canvas, (TrackPoint[])trackLog.toArray(new TrackPoint[size]), 0, size);
        //lineChart.drawLine(canvas, trackPoints, 0, size);
        if(canvas == null)  Log.d(TAG, "canvas null");
        for(int i=0; i<size; i++){
            Log.d(TAG, "point " + i + ":" + trackPoints.getVal(TrackPoints.VAL_ALTITUDE, i));
        }

        bUpdated = false;

//        this.index = idx;
//        this.size = size;
//
//        if(lineChart == null) Log.d(TAG, "lineChart NULL");
//        else if(surfaceHolder == null) Log.d(TAG, "surfaceHolder NULL");
//        else if(trackLog == null) Log.d(TAG, "trackLog NULL");
//        else {
//            Canvas canvas = surfaceHolder.lockCanvas();
//            if(canvas == null) Log.d(TAG, "canvas NULL");
//            else
//                surfaceHolder.unlockCanvasAndPost(canvas);
//        }
    }

    public class ShowThread extends Thread {

        //Canvas canvas;
        boolean flag = false;
        int span = 1000;

        //constructor
        public ShowThread() {
            flag = true;
        }

        public void run() {
            while(flag) {
                try {
                    if(!bUpdated)  continue;//Thread.sleep(span);
                    if(size == trackPoints.getSize())  continue;//Thread.sleep(span);

                    //Bundle drawBundle = new Bundle();
                    //drawBundle;
                    Log.d(TAG, "trackPoints size " + trackPoints.getSize());
                    Canvas canvas = surfaceHolder.lockCanvas();
                    if(canvas == null)  Log.d(TAG, "canvas 0 null");
                    Message msg = new Message();
                    msg.what = MSG_DRAW;
                    //msg.setData(countBundle);
                    showHandle.sendMessage(msg);

//                    synchronized(surfaceHolder) {
//                        canvas = surfaceHolder.lockCanvas();
//
//                        doDraw(canvas);
//
//                        surfaceHolder.unlockCanvasAndPost(canvas);
//                    }

                    Thread.sleep(span);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
*/
